package com.example.downloaddemo;

import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;

/**
 * Created by xiewei on 15-2-4.
 */
public class ProgressAdapter2 extends BaseAdapter{
	private  List<String> list;
	private DownLoader loader;
	private LoadInfo info;
	private Handler handler;
	private String path;
	private HashMap<String, DownLoader> loaders;
	private HashMap<String, ProgressBar> bars;
	public ProgressAdapter2(List<String> urllist, Handler handler, HashMap<String, DownLoader> loaders, HashMap<String, ProgressBar> bars){
		this.list=urllist;
		this.handler=handler;
		this.loaders=loaders;
		path= Environment.getExternalStorageDirectory().getPath()+"/downloaddemo";
		this.bars =bars;
	}
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, final ViewGroup parent) {
		ViewHolder holder;
		if(convertView==null){
			convertView=View.inflate(parent.getContext(),R.layout.item,null);
			holder=new ViewHolder();
			holder.tv_name= (TextView) convertView.findViewById(R.id.tv_name);
			holder.progress= (ProgressBar) convertView.findViewById(R.id.progress);
			holder.down= (Button) convertView.findViewById(R.id.down);
			holder.stop= (Button) convertView.findViewById(R.id.stop);
			convertView.setTag(holder);
		}else{
			holder= (ViewHolder) convertView.getTag();
		}

		final String url=list.get(position);
		holder.tv_name.setText(url.substring(url.lastIndexOf("/")+1));
		final Button b=holder.down;
		final ProgressBar p=holder.progress;
		p.setProgress(0);

		if(loaders.get(url)==null||loader.state!=Constants.DOWNLOADING){
			b.setText("下载");
		}else {
			b.setText("暂停");
		}
		holder.down.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				loader = loaders.get(list.get(position));
				if (loader == null) {
					loader = new DownLoader(list.get(position), path, 2, parent.getContext(), handler);
					loaders.put(list.get(position), loader);
				}
				bars.put(url, p);
				if ("暂停".equals(b.getText())) {
					loader.pause();
					b.setText("下载");
				} else {
					b.setText("暂停");
					new Thread() {
						@Override
						public void run() {
							Log.i("msg", "loader:" + loader);
							info = loader.getLoadInfo();
							Message msg = new Message();
							msg.what = 2;
							msg.obj = info;
							handler.sendMessage(msg);
							loader.download();
						}
					}.start();
				}
			}
		});
		return convertView;
	}
	public static class ViewHolder{
		TextView tv_name;
		ProgressBar progress;
		Button down;
		Button stop;
	}
}
